<?php 
/* P3(c)

******************************************************************************
                                                                                      
                         __                                                        
                        /__)                                                       
                       /__        P3 - moving Technologie            
                        __)                                                          
                        __)                                                          
                                                                                        
                                                                                        
       Copyright 2009 by p3.co.at                                                                                 
                                                                                        
       visit www.p3.co.at to get more Informations 
       about Teriko - Betriebsdatenerfassung                                                                                 
       - it's not allowed to change this scripts
       - it's not allowed to remove this copyright-tag

       -- you can download the complete license   
          under http://www.teriko.org

 (c) by Patrik Pfaffenbauer and P3 (http://www.p3.co.at)

 | Programname: P3.Teriko
 | Authtor: P3.Verein
 | Coder: Patrik Pfaffenbauer
 | Version: 0.1

 | Change Index
 |_____________________________________________
 |                                             |
 |4.3.2010 Erstellung | Patrik Pfaffenbauer    |
 |_____________________________________________|

******************************************************************************

P3(c) */

class Online extends Base
{
	public function Online()
	{
		$this->title = $GLOBALS['lang']->GetLanguage("Online", "title");
		$this->toolbar = true;
		$this->moduleID = 400;
		$this->permission = GetPermission($this->moduleID);
		
		if(PermissionCount($this->permission) == 0)
		{
			$this->allowed = false;
		}
	}
	
	public function Add()
	{		

	}
	public function Edit()
	{		

	}
	
	public function AnalysPost()
	{
		include("./_tables.php");
		
		if($_POST['action'] == "startJob")
		{
			$query = "UPDATE ".$tblpausetimes." SET endTime=NOW(), endTimeEdit=NOW() WHERE userid=".$_SESSION['userid']." AND endTime IS NULL";
			$GLOBALS['db']->Query($query);
			
			$query = "UPDATE ".$tbljobtimes." SET endTime=Now(), endTimeEdit=Now() WHERE userid='".$_SESSION['userid']."' AND endTime IS NULL";
			$GLOBALS['db']->Query($query);
			
			$query = "INSERT INTO ".$tbljobtimes." (userid, jobid, startTime, startTimeEdit, comment) VALUES (".$_SESSION['userid'].", ".$_POST['jobID'].", Now(), Now(), '".$_POST['commentTextActualJob']."')";
			$GLOBALS['db']->Query($query);
			
			echo '<script type="text/javascript">
			Redirect("index.php?s=online&jobid='.$_POST['jobID'].'");
			</script>';
		}
		else
		{
			$this->Paint();
		}
	}

	public function Paint()
	{
		include("_tables.php");
		$divCount = 0;
		
		if(isset($_GET['jobid']))
		{
			$res = $GLOBALS['db']->Fetch($GLOBALS['db']->Query("SELECT * FROM ".$tbljobs." WHERE id=".$_GET['jobid']));
			$res2 = $GLOBALS['db']->Fetch($GLOBALS['db']->Query("SELECT * FROM ".$tblproject." WHERE id=".$res->projectID));
			$res3 = $GLOBALS['db']->Fetch($GLOBALS['db']->Query("SELECT * FROM ".$tblcustomer." WHERE id=".$res2->customerID));
			
			$jobid = $_GET['jobid'];
			$projectid = $res2->id;
			$customerid = $res3->id;
			
			$i = 0;
			$j = 0;
			$customer = $GLOBALS['db']->Query("SELECT * FROM ".$tblcustomer." order by name");
			
			while($res = $GLOBALS['db']->Fetch($customer))
			{
				$project = $GLOBALS['db']->Query("SELECT * FROM ".$tblproject. " WHERE customerID=".$res->id." order by name");
				
				if($res->id == $customerid)
					$customerCount = $i;
				
				while($res2 = $GLOBALS['db']->Fetch($project))
				{
					if($res2->id == $projectid)
						$projectCount = $j;
				
					$j++;
				}
				$i++;
			}
		}
		
		echo '
		<table class="adminTable" name="primTable">
		<tr>
		<td width="400px" valign="top">
		<table border="0" cellspacing="0" cellpadding="0">
        	<tr> 
          		<td>
          			<img src="images/tree/user.png" width="16" height="16" alt="'.$GLOBALS['lang']->GetLanguage("Customer", "title").'"/>
          				'.$GLOBALS['lang']->GetLanguage("Customer", "title").'
          		</td>
          	</tr>';
		
		$customer = $GLOBALS['db']->Query("SELECT * FROM ".$tblcustomer." order by name");
		$i = 0;
		$j = 0;
		$y = 0;
		while($res = $GLOBALS['db']->Fetch($customer))
		{
			$project = $GLOBALS['db']->Query("SELECT * FROM ".$tblproject. " WHERE customerID=".$res->id." order by name");
			
			if(isset($_GET['jobid']))
			{
				if($j != $projectCount)
					$img = "plus.gif";
				else
					$img = "minus.gif";
			}
			else
			{
				$img = "plus.gif";
			}
			
			echo '
			
			<tr valign="top">
				<td valign="top">	
					<a onClick="ToggleTree(this)" class="tree_folder">
						<div id="divImg'.$divCount.'">
							<img src="images/tree/'.$img.'" alt="link" />
							<img src="images/tree/user.png" alt="link" />
							'.$res->name.'
						</div>
					</a>';
			
			if(isset($_GET['jobid']))
			{
				if($i != $customerCount)
					echo '<div style="display:none;" id="divTree'.$divCount.'">';
				else
					echo '<div id="divTree'.$divCount.'">';
			}
			else
			{
				echo '<div style="display:none;" id="divTree'.$divCount.'">';
			}
			
			$divCount++;
			
			while($res2 = $GLOBALS['db']->Fetch($project))
			{
				$job = $GLOBALS['db']->Query("SELECT * FROM ".$tbljobs." WHERE projectID=".$res2->id." order by name");
				
				if(isset($_GET['jobid']))
				{
					if($j != $projectCount)
						$img = "plus.gif";
					else
						$img = "minus.gif";
				}
				else
				{
					$img = "plus.gif";
				}
				
				echo '
				
				 <table border="0" cellspacing="0" cellpadding="0">
                	<tr> 
                  		<td width="15"></td>
                  		<td>
                  			<a onClick="ToggleTree(this)" class="tree_folder">
	                  			<div id="divImg'.$divCount.'">
	                  				<img src="images/tree/'.$img.'" alt="link" />
	                  				<img src="images/tree/project.png" alt="link" />
	                  				'.$res2->name.'
	                  			</div>
                  			</a> ';
				
				if(isset($_GET['jobid']))
				{
					if($j != $projectCount)
						echo '<div style="display:none;" id="divTree'.$divCount.'">';
					else
						echo '<div id="divTree'.$divCount.'">';
				}
				else
				{
					echo '<div style="display:none;" id="divTree'.$divCount.'">';
				}
				
				$divCount++;
				
				while($res3 = $GLOBALS['db']->Fetch($job))
				{
					if($res3->id == $jobid)
					{
						$class = "tree_leaf_selected";	
						$img = "39.png";			
					}
					else
					{
						$img = "38.png";	
						$class = "tree_leaf";
					}
					
					echo '
					<table border="0" cellspacing="0" cellpadding="0">
                        <tr> 
                          <td width="15"></td>
                          <td>
	                      	<a class="'.$class.'" onClick="LoadJob(\''.$res3->id.'\');">
	                        	<img src="images/tree/link.gif" alt="link" />
	                        	<img src="images/tree/'.$img.'" alt="link" />
								'.$res3->name.'
	                        </a>
                          </td>
                        </tr>
                      </table>';
					$y++;
				}
				echo '</div></td></tr></table>';
				$j++;
			}
			echo '</div></td></tr>';
			$i++;
		}
		echo '
		<input type="hidden" name="divCount" id="divCount" value="'.$divCount.'" />
		<tr>
			<td>
				<br/>
			</td>
		</tr>
		<tr>
			<td>
				<a href="javascript:void(0)" onClick="ToggleAll(true);">'.$GLOBALS['lang']->GetLanguage("Common", "collapseDown").'</a>
				<a href="javascript:void(0)" onClick="ToggleAll(false);">'.$GLOBALS['lang']->GetLanguage("Common", "collapseUp").'</a>
			</td>	
		</tr>
		</table></td><td valign="top"><div id="actualJob">';
		
		if(isset($_GET['jobid']))
		{
			$this->PaintActualJob($_GET['jobid']);
		}
		else
		{
			$GLOBALS['lang']->GetLanguage("Online", "chooseJob");
		}
		echo '</div>
		</td></tr></table>';
		
	}
	
	public function PaintActualJob($jobID)
	{
		include("./_tables.php");
		$res = $GLOBALS['db']->Fetch($GLOBALS['db']->Query("SELECT *,DATE_FORMAT(endDate,'%d.%m.%Y') as end,DATE_FORMAT(beginDate,'%d.%m.%Y') as begin FROM ".$tbljobs." WHERE id=".$jobID));
		$res2 = $GLOBALS['db']->Fetch($GLOBALS['db']->Query("SELECT * FROM ".$tblproject." WHERE id=".$res->projectID));
		$res3 = $GLOBALS['db']->Fetch($GLOBALS['db']->Query("SELECT * FROM ".$tblcustomer." WHERE id=".$res2->customerID));
		
		$jobtimes = $GLOBALS['db']->Fetch($GLOBALS['db']->Query("SELECT * FROM ".$tbljobtimes." WHERE userid=".$_SESSION['userid']." AND jobid=".$jobID." AND endTime IS NULL"));
			
		$comment = str_replace("<br />", "\n",$jobtimes->comment);
		
		echo '
		<form action="index.php?s=online" method="post" name="adminForm" style="width:400px">
			<table class="adminform">
				<tr>
					<td width="100px" valign="top">
						<strong>
							'.$GLOBALS['lang']->GetLanguage("Customer", "title2").':
						</strong>
					</td>
					<td valign="top">
						'.$res3->name.'
					</td>
				</tr>
				<tr>
					<td width="100px" valign="top">
						<strong>
							'.$GLOBALS['lang']->GetLanguage("Project", "name").':
						</strong>
					</td>
					<td valign="top">
						'.$res2->name.'
					</td>
				</tr>
				<tr>
					<td>
						<br />
					</td>
				</tr>
				<tr>
					<td width="100px" valign="top">
						<strong>
							'.$GLOBALS['lang']->GetLanguage("Task", "title").':
						</strong>
					</td>
					<td valign="top">
						'.$res->name.'
					</td>
				</tr>
				<tr>
					<td width="100px" valign="top">
						<strong>
							'.$GLOBALS['lang']->GetLanguage("Common", "description").':
						</strong>
					</td>
					<td valign="top">
						'.htmloutput($res->description).'
					</td>
				</tr>
				<tr>
					<td width="100px" valign="top">
						<strong>
							'.$GLOBALS['lang']->GetLanguage("Common", "beginDate").':
						</strong>
					</td>
					<td valign="top">
						'.$res->begin.'
					</td>
				</tr>
				<tr>
					<td width="100px" valign="top">
						<strong>
							'.$GLOBALS['lang']->GetLanguage("Common", "endDate").':
						</strong>
					</td>
					<td valign="top">
						'.$res->end.'
					</td>
				</tr>
				<tr>
					<td width="100px" valign="top">
						<strong>
							'.$GLOBALS['lang']->GetLanguage("Common", "comment").':
						</strong>
					</td>
					<td>
						<textarea name="commentTextActualJob" id="commentTextActualJob" cols="30" rows="5">'.$comment.'</textarea>
					</td>
				</tr>
				<tr>
					<td colspan="2">
						<br />
						<center>';
		
		if($jobtimes->jobID == $jobID)
		{
			echo '<a href="javascript:void(0)" onClick="CommentRunningJob(\''.$jobID.'\', \'commentTextActualJob\', \'commentText\', \'commentResultActualJob\')">'.$GLOBALS['lang']->GetLanguage("Online", "commentJob").'</a>
			<div id="commentResultActualJob"></div>';
		}
		else
		{
			echo '<a href="javascript:void(0)" onClick="CheckCommentIsSet(\'startJob\')">'.$GLOBALS['lang']->GetLanguage("Online", "runJob").'</a>';
		}
		
		echo'			</center>
					</td>
				</tr>
			</table>
			<input type="hidden" name="action" value="startJob" />
			<input type="hidden" name="jobID" value="'.$res->id.'" />
			<div id="dummy"></div>
		</form>';
		
		//echo $res->name;
	}
	
	public function StartLastJob($comment)
	{
		include("./_tables.php");
		
		$query = "SELECT *,DATE_FORMAT(startTime,'%d.%m.%Y %H:%i') as start FROM ".$tbljobtimes." WHERE userID = ".$_SESSION['userid']." order by endTime desc limit 0,1";
		$res = $GLOBALS['db']->Fetch($GLOBALS['db']->Query($query));
		
		$query = "UPDATE ".$tblpausetimes." SET endTime=NOW(), endTimeEdit=NOW() WHERE userid=".$_SESSION['userid']." AND endTime IS NULL";
		$GLOBALS['db']->Query($query);
			
		$query = "INSERT INTO ".$tbljobtimes." (userid, jobid, startTime, startTimeEdit, comment) VALUES (".$_SESSION['userid'].", ".$res->jobID.", Now(), Now(), '".$comment."')";
		$GLOBALS['db']->Query($query);
		
		echo $GLOBALS['lang']->GetLanguage("Common", "waitForReload");
	}
	
	public function StopJob($jobID, $comment)
	{
		include("./_tables.php");
		
		$comment =str_replace("%*%", "&",$comment);
		
		$query = "UPDATE ".$tbljobtimes." SET endTime=Now(), endTimeEdit=Now(), comment='".$comment."' WHERE userid='".$_SESSION['userid']."' AND endTime IS NULL AND jobID=".$jobID;
		$GLOBALS['db']->Query($query);
		
		echo $GLOBALS['lang']->GetLanguage("Common", "waitForReload");
	}
	public function PauseJob($jobID, $pauseid)
	{
		include("./_tables.php");
		
		$query = "INSERT INTO ".$tblpausetimes." (pauseid, userid, startTime, startTimeEdit) VALUES (".$pauseid.", ".$_SESSION['userid'].", NOW(), NOW())";
		$GLOBALS['db']->Query($query);
		
		$query2 = "UPDATE ".$tbljobtimes." SET endTime=Now(), endTimeEdit=Now() WHERE endTime IS NULL AND userid=".$_SESSION['userid']." AND jobid=".$jobID;
		$GLOBALS['db']->Query($query2);
		
		echo $GLOBALS['lang']->GetLanguage("Common", "waitForReload");
	}
	public function CommentJob($jobID, $comment)
	{
		include("./_tables.php");
		
		$comment =str_replace("%*%", "&",$comment);
		
		$query = "UPDATE ".$tbljobtimes." SET comment='".$comment."' WHERE userid=".$_SESSION['userid']." AND jobID=".$jobID." AND endTime IS NULL";
		$GLOBALS['db']->Query($query);
		//echo $comment;
		echo str_replace('$date', date("m.d.Y H:i"), $GLOBALS['lang']->GetLanguage("Online", "commentUpdated"));
	}
	
	public function Toolbar()
	{	
		echo '
		<td class="button" id="toolbar-help">
		<a href="#" onclick="OpenHelpMenu(\''.$this->moduleID.'\',\''.$_GET['s'].'\')" class="toolbar">
		<span class="icon-32-help" title="'.$GLOBALS['lang']->GetLanguage("Common", "help").'">
		</span>
		Hilfe
		</a>
		</td>';
	}

	public function AddCSS()
	{
		echo '<link rel="stylesheet" type="text/css" href="css/base/treeview.css" />';	
	}
	public function AddJS()
	{
		
	}
}

?>